package com.gsx.Controller;

import com.github.pagehelper.PageInfo;
import com.gsx.entity.*;
import com.gsx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/")
    public String index(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                        @RequestParam(name="size",required = true,defaultValue = "5")int size, Model model,
                        HttpSession session, Authentication authentication){
        /*需要重写 简化操作以免查询太多*/
        /*List<Blog> blogList=blogService.getAllBlog(page, size);
        List<User>  userList=blogService.getBlogUser();
        for(int i=0;i<size;i++){
            blogList.get(i).setUser(userList.get(i+page*size));
        }*/
        /*博客的分页的数据*/
        PageInfo pageInfo=new PageInfo(blogService.getBlog_Type_User(page,size));
        model.addAttribute("pageInfo",pageInfo);
        /*Type的数据*/
        model.addAttribute("types",typeService.getAll());
        /*Tag的数据*/
        model.addAttribute("tags",tagService.getAll());
        /*最新的六条博客*/
        model.addAttribute("nBlogs",blogService.getNewBlog());
        return "index";
    }

    @PostMapping("/search")
    public String search(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                         @RequestParam(name="size",required = true,defaultValue = "5")int size, Model model,@RequestParam String query){
        PageInfo pageInfo=new PageInfo(blogService.getByBlogTitle(page,size,query));
        model.addAttribute("pageInfo",pageInfo);
        /*再查询出结果之后  下一个页面仍然能显示query*/
        model.addAttribute("query",query);
        return "search";
    }

    @GetMapping("/blog/{id}")
    public String blog(Model model, @PathVariable Long id){
        Blog_Type_User blog=blogService.getB_T_U_Front(id);
        Blog blog1=blogService.getBlog(id);
        blog1.setViews(blog1.getViews()+1);
        blogService.updateBlog(blog1);
        model.addAttribute("blog",blog);
        model.addAttribute("comments",commentService.getCommentListById(id));
        model.addAttribute("tags",tagService.getBlog_tag(blog.getBlog_id()));
        return "blog";
    }

    /*前端的tag展示页面*/

    @GetMapping("/tags")
    public String tag(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                        @RequestParam(name="size",required = true,defaultValue = "5")int size, Model model){
        /*博客的分页的数据*/
        PageInfo pageInfo=new PageInfo(blogService.getBlog_Type_User(page,size));
        model.addAttribute("pageInfo",pageInfo);
        /*Tag的数据*/
        model.addAttribute("tags",tagService.getAll());
        return "tags";
    }

    @GetMapping("/tags/{id}")
    public String getTag(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                         @RequestParam(name="size",required = true,defaultValue = "5")int size,
                         Model model,@PathVariable Long id){
        PageInfo pageInfo=new PageInfo(tagService.getBlog_Tags(id));
        model.addAttribute("pageInfo",pageInfo);
        /*Tag的数据*/
        model.addAttribute("tags",tagService.getAll());
        return "tags";
    }

    /*前端的type展示页面*/
    @GetMapping("/types")
    public String type(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                      @RequestParam(name="size",required = true,defaultValue = "5")int size, Model model){
        /*博客的分页的数据*/
        PageInfo pageInfo=new PageInfo(blogService.getBlog_Type_User(page,size));
        model.addAttribute("pageInfo",pageInfo);
        /*Tag的数据*/
        model.addAttribute("types",typeService.getAll());
        return "types";
    }

    @GetMapping("/types/{id}")
    public String getType(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                         @RequestParam(name="size",required = true,defaultValue = "5")int size,
                         Model model,@PathVariable Long id){
        PageInfo pageInfo=new PageInfo(typeService.getBlog_Type_User(id));
        model.addAttribute("pageInfo",pageInfo);
        /*Tag的数据*/
        model.addAttribute("types",typeService.getAll());
        return "types";
    }

    /*归档功能*/
    @GetMapping("/archives")
    public String archive(Model model){
        /*获取有的年份*/
        List<Years> yearsList = new ArrayList<Years>();
        String[] years=blogService.getYears();
        for(String year : years) {
            Years years1=new Years();
            years1.setBlogList(blogService.getByYear(year));
            years1.setYear(year);
            yearsList.add(years1);
        }
        model.addAttribute("years",yearsList);
        return "archives";
    }

    /*关于我*/
    @GetMapping("/about")
    public String about(){
        return "about";
    }

}
